﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;


public partial class CustomerManagement_CustomerTypeChangeManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400108");
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            BindDropCreaterAndChecker();
            BindDataList();
        }

        Session["FCode"] = "300400108";
    }

    private void BindDropCreaterAndChecker()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        if (users != null)
        {
            dropChecker.DataSource = users;
            dropChecker.DataTextField = "USERS_NAME";
            dropChecker.DataValueField = "USERS_CODE";
            dropChecker.DataBind();

            dropCreater.DataSource = users;
            dropCreater.DataTextField = "USERS_NAME";
            dropCreater.DataValueField = "USERS_CODE";
            dropCreater.DataBind();

            ListItem item = new ListItem();
            item.Text = "全部";
            item.Value = "-1";

            dropChecker.Items.Insert(0, item);
            dropCreater.Items.Insert(0, item);
        }
    }

    private void BindDataList()
    {
        try
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(txtBillId.Text.Trim() != "" ? " and Change_ID like '%" + txtBillId.Text.Trim() + "%' " : "");
            strSql.Append(dropCreater.SelectedIndex != 0 ? " and Change_Creator ='" + dropCreater.SelectedValue + "' " : "");
            strSql.Append(txtCheckDateStart.Text.Trim() != "" ? " and Change_CreateDate >= '" + txtCheckDateStart.Text.Trim() + " ' " : "");
            strSql.Append(txtCheckDateEnd.Text.Trim() != "" ? " and Change_CreateDate <= '" + txtCheckDateEnd.Text.Trim() + " ' " : "");
            strSql.Append(dropAudit.SelectedIndex != 0 ? " and Change_Check=" + dropAudit.SelectedValue + " " : "");
            strSql.Append(txtCreateDateStart.Text.Trim() != "" ? " and Change_CheckDate >='" + txtCreateDateStart.Text.Trim() + "' " : "");
            strSql.Append(txtCreateDateEnd.Text.Trim() != "" ? " and Change_CheckDate <='" + txtCreateDateEnd.Text.Trim() + "' " : "");
            strSql.Append(dropChecker.SelectedIndex != 0 ? " and Change_Checker='" + dropChecker.SelectedValue + "' " : "");
            strSql.Append(txtCustomerId.Text.Trim() == "" ? "" : "and C_CustomerID like '%" + txtCustomerId.Text.Trim() + "%' ");

            int recordCount;
            DataSet dst = new BLL.CustomerTypeChangeDocMain().GetMain(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, strSql.ToString(), out recordCount);
            dlistCustomerTypeChange.DataSource = dst;
            dlistCustomerTypeChange.DataBind();
            AspNetPager1.RecordCount = recordCount;
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }

    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }

    

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400108", "00001"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        Response.Redirect("AddCustomerTypeChangeManagement.aspx");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataList();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindDataList();
    }
}
